package com.heny.demo.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heny.demo.common.BaseDataServiceBean;
import com.heny.demo.common.CommonErrorCode;
import com.heny.demo.common.CommonException;
import com.heny.demo.entity.DataStatisticsModel;
import com.heny.demo.mapper.DataStatisticsModelMapper;
import com.heny.demo.model.DataConvertModel;
import com.heny.demo.model.enu.AggregationTypeEnum;
import com.heny.demo.model.enu.DefaultValueTypeEnum;
import com.heny.demo.service.BaseDataService;
import com.heny.demo.service.DataStatisticsModelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author litao.cc
 * @version DataStatisticsModelServiceImpl.java v0.1 t2025/5/6 20:58
 * @date 2025/5/6 20:58
 * @description DataStatisticsModelServiceimpl
 */
@Service
public class DataStatisticsModelServiceImpl extends ServiceImpl<DataStatisticsModelMapper, DataStatisticsModel> implements DataStatisticsModelService {

    @Autowired
    BaseDataServiceBean dataServiceBean;

    @Override
    public void exeModel(Long id) {
        // 取出模型数
        DataStatisticsModel model = this.getById(id);
        if(model ==  null){
            throw new CommonException(CommonErrorCode.PARAM_VALIDATION_FAILED,"model is null");
        }

        // 获取模型对应的数据库读写bean
        BaseDataService baseDataService = dataServiceBean.getBaseDataService(model.getDataType(), model.getStatisticsType());

        // 取数base数据
        List<Map<String, Object>> dbData = baseDataService.query(model);


        String dataMappingConfigString = model.getDataMappingConfig();
        List<DataConvertModel> dataConvertModels = JSONArray.parseArray(dataMappingConfigString, DataConvertModel.class);
        // 数据转换,默认值赋值
        Map<String, List<Double>> baseData = convertBaseData(dbData, dataConvertModels);


        // 聚合
        Map<String,Double> aggDataMap = new HashMap<>();
        dataConvertModels.forEach(item->{
            String aggType = item.getAggType();
            Double calculate = AggregationTypeEnum.calculate(aggType, baseData.getOrDefault(item.getReq(), new ArrayList<>()));
            aggDataMap.put(item.getRes(),calculate);
        });

        // 存储数据
        baseDataService.save(model,aggDataMap);

    }


    private Map<String,List<Double>> convertBaseData(List<Map<String, Object>> dbData,List<DataConvertModel> dataConvertModels){
        Map<String,List<Double>> data = new HashMap<>();
        dataConvertModels.forEach(item->{
            data.put(item.getReq(),new ArrayList<>());
        });
        for (int i = 0; i < dbData.size(); i++) {
            Map<String, Object> row = dbData.get(i);
            dataConvertModels.forEach(item->{
                String res = item.getReq();
                if(row.containsKey(res)){
                    // TODO 强制转成double,后续要考虑支持多种数据类型
                    data.get(item.getReq()).add( (Double)row.get(res));
                }else{
                    // 处理默认值
                    if(item.getDefaultValueType().equals(DefaultValueTypeEnum.ZERO)){
                        data.get(item.getReq()).add(0D);
                    }else if(item.getDefaultValueType().equals(DefaultValueTypeEnum.CUSTOM)){
                        data.get(item.getReq()).add(item.getCustomDefaultValue());
                    }
                }
            });

        }
        return data;
    }
}
